package org.boyan.query;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.boyan.entity.query.IndexColumn;
import org.boyan.entity.query.OnlineDeviseScheme;


import java.util.ArrayList;
import java.util.List;

public class ExcelToOnlineDeviseScheme {

    //解析excel联机设计sheet的方案表
    public static OnlineDeviseScheme getOnlineDeviseScheme(Sheet sheet){
        OnlineDeviseScheme onlineDeviseScheme=new OnlineDeviseScheme();
        String chooser=sheet.getRow(0).getCell(1).getStringCellValue();
        onlineDeviseScheme.setChooser(chooser);
        List<IndexColumn> indexColumnList=new ArrayList<>();
        try{
            int startRow=getStartRow(sheet);
            if(startRow==-1){
                throw new Exception("文件解析失败，看下是否上传错误?");
            }
            if("Hbase+Solr".equals(chooser)){
                indexColumnList=getSolr(sheet);
                onlineDeviseScheme.setIndexName(sheet.getRow(startRow-2).getCell(1).getStringCellValue());
                onlineDeviseScheme.setAmount(sheet.getRow(startRow-1).getCell(1).getStringCellValue());
            }else if("Hbase+ES".equals(chooser)){
                onlineDeviseScheme.setIndexName(sheet.getRow(startRow-2).getCell(5).getStringCellValue());
                onlineDeviseScheme.setAmount(sheet.getRow(startRow-1).getCell(5).getStringCellValue());
                indexColumnList=getES(sheet);
            }
            onlineDeviseScheme.setIndexColumnList(indexColumnList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return onlineDeviseScheme;
    }

    //定位索引字段的起始位置
    private static int getStartRow(Sheet sheet){
        for(Row row:sheet){
            if(row.getCell(0)==null){
                continue;
            }
            if("索引字段名".equals(row.getCell(0).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //解析solr方案
    private static List<IndexColumn> getSolr(Sheet sheet){
        List<IndexColumn> indexColumnList=new ArrayList<>();
        try{
            int startRow=getStartRow(sheet);
            if(startRow==-1){
                throw new Exception("解析错误,看看文件是否传错了?");
            }
            int endRow=sheet.getLastRowNum();
            for(int i=startRow+1;i<=endRow;i++){
                if("".equals(sheet.getRow(i).getCell(0).getStringCellValue())){
                    continue;
                }
                IndexColumn indexColumn=new IndexColumn();
                indexColumn.setColumnName(sheet.getRow(i).getCell(0).getStringCellValue());
                indexColumn.setColumnType(sheet.getRow(i).getCell(1).getStringCellValue());
                String saveFlag=sheet.getRow(i).getCell(2).getStringCellValue();
                if("是".equals(saveFlag)||"Y".equals(saveFlag)){
                    indexColumn.setSaveFlag(true);
                }else{
                    indexColumn.setSaveFlag(false);
                }
                indexColumn.setColumnPurpose(sheet.getRow(i).getCell(3).getStringCellValue());
                indexColumnList.add(indexColumn);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return indexColumnList;
    }

    //解析ES方案
    private static List<IndexColumn> getES(Sheet sheet){
        List<IndexColumn> indexColumnList=new ArrayList<>();
        try{
            int startRow=getStartRow(sheet);
            if(startRow==-1){
                throw new Exception("解析错误,看看文件是否传错了?");
            }
            int endRow=sheet.getLastRowNum();
            for(int i=startRow+1;i<=endRow;i++){
                if("".equals(sheet.getRow(i).getCell(4).getStringCellValue())){
                    continue;
                }
                IndexColumn indexColumn=new IndexColumn();
                indexColumn.setColumnName(sheet.getRow(i).getCell(4).getStringCellValue());
                indexColumn.setColumnType(sheet.getRow(i).getCell(5).getStringCellValue());
                String saveFlag=sheet.getRow(i).getCell(6).getStringCellValue();
                if("是".equals(saveFlag)||"Y".equals(saveFlag)){
                    indexColumn.setSaveFlag(true);
                }else{
                    indexColumn.setSaveFlag(false);
                }
                indexColumn.setColumnPurpose(sheet.getRow(i).getCell(7).getStringCellValue());
                indexColumnList.add(indexColumn);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return indexColumnList;
    }

}
